﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlockMe.Infrastructure;
using BlockMe.Model;
using Alpha.DataBase;
using MySql.Data.MySqlClient;

namespace BlockMe.Repository.MySQL
{
    public class BlogRepository : IBlogRepository
    {
        DBProvider provider = ProviderFactory.GetProvider();
        private static Blog blog = null;

        public string TableName { get; set; }

        public BlogRepository()
        {
            TableName = "blog_settings";
        }

        public Blog GetSettings()
        {
            if (blog==null)
            {
                var reader = provider.ExecuteDataReader("select setting_name,setting_value from " + TableName);
                while (reader.Read())
                {
                    blog.Settings[reader.GetString(0)] = reader.GetString(1);
                }
                reader.Close();
                reader.Dispose();
                blog.BlogTitle = blog.Settings["blog_title"];
                blog.SiteBaseUrl = blog.Settings["site_url"];
                blog.BlogUrl = blog.Settings["blog_url"];
                blog.Desc = blog.Settings["blog_desc"];

            }
            return blog;
        }

        public void UpdateSetting(string key, string value)
        {
            try
            {
                var ps = new MySqlParameter[] { 
                    new MySqlParameter{ DbType = System.Data.DbType.String, Value = key, ParameterName = "@setting_name"},
                    new MySqlParameter{ DbType = System.Data.DbType.String, Value = value, ParameterName ="@setting_value"}
                };

                provider.ExecuteNonQuery("update " + TableName + " set setting_value=@setting_value where setting_name=@setting_name", ps);
            }
            catch (Exception e)
            {
            }
        }

        public string this[string index]
        {
            get {
                if (blog==null)
                {
                    GetSettings();
                }
                if (blog.Settings.ContainsKey(index))
                {
                    return blog.Settings[index];
                }
                else
                {
                    return null;
                }
            }
            set {
                if (blog == null)
                {
                    GetSettings();
                }
                if (blog.Settings.ContainsKey(index))
                {
                    blog.Settings[index] = value;
                    UpdateSetting(index, value);
                }
                else
                {
                    blog.Settings[index] = value;
                    AddSetting(index, value);
                }
            }
        }


        public void AddSetting(string key, string value)
        {
            try
            {
                var ps = new MySqlParameter[] { 
                    new MySqlParameter{ DbType = System.Data.DbType.String, Value = key, ParameterName = "@setting_name"},
                    new MySqlParameter{ DbType = System.Data.DbType.String, Value = value, ParameterName ="@setting_value"}
                };
                provider.ExecuteNonQuery("insert into " + TableName + " (setting_name,setting_value) values (@setting_name,@setting_value)");
            }
            catch (Exception e)
            {
            }
        }


        public void Remove(string key)
        {
            try
            {
                var ps = new MySqlParameter[] { 
                    new MySqlParameter{ DbType = System.Data.DbType.String, Value = key, ParameterName = "@setting_name"},
                };

                provider.ExecuteNonQuery("delete from " + TableName + " where setting_name=@setting_name", ps);
            }
            catch (Exception e)
            {
            }
        }


    }
}
